package com.ruoyi.kis.controller;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult_KIS;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.domain.ThtzdF;
import com.ruoyi.kis.domain.TltzdF;
import com.ruoyi.kis.domain.ZxrkF;
import com.ruoyi.kis.service.IKisService;
import com.ruoyi.kis.util.HttpSendWmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author: zhanglm
 * @CreateDate: 2025-03-04 14:59
 * @FullNameClass: com.ruoyi.kis.controller.KisZxController
 * @Description: 退货单
 *
 *
 */

@RestController
@RequestMapping("/kisth")
@Component("kisth")
@Api("kis退货通知单接口")
public class KisThtzController extends BaseController {



    @Autowired
    private IKisService kisService;

    private static final Logger LOGGER = LoggerFactory.getLogger(KisThtzController.class);


    /**
     *
     *
     * FranType: 退货通知单，单据类型82
     * @return ERP >> WMS
     */
    @ApiOperation("退货通知单")
    @RequestMapping("/thtzd")
    public AjaxResult_KIS thtzd(){
        List<ThtzdF> thtzdFList = kisService.querythtzdF();
        String s  = "";
        if (thtzdFList != null && thtzdFList.size() > 0){
            String fBillNo = thtzdFList.get(0).getFBillNo();
            s = JSONUtil.parse(thtzdFList.get(0)).toString();
            LOGGER.info("退货通知单 ERP >> WMS-JSON:{}",s);
            try {
               // String msg = HttpUtil.post(Constants.THTZD_URL,s);
                String msg = HttpSendWmsUtil.sendWms(Constants.THTZD_URL,s);
                JSONObject entries = JSONUtil.parseObj(msg);
                String msgcode = entries.get("msgcode").toString();
                String kisFlag;
                if (msgcode.equals(Constants.ZHIKU.MSGCODE_SUCCESS)){
                    kisFlag = Constants.ZHIKU.KISFLAG_SUCCESS;
                }else {
                    kisFlag = Constants.ZHIKU.KISFLAG_FAILURE;
                }
                int updateCount = kisService.updateKisSEOutStockFlag(thtzdFList.get(0).getFInterID(),kisFlag);
                LOGGER.info("退货通知单{}, ERP >> WMS:更新kisFlag状态：{},更新:{}条,反馈：{}",fBillNo,kisFlag,updateCount,msg);
            }catch (HttpException e){
                LOGGER.error("退货通知单失败" + e.getMessage());
                return AjaxResult_KIS.error("退货通知单失败"+e.getMessage());
            }
        }
        return AjaxResult_KIS.success("OK","");
    }

}
